package apjava.basico;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServidorModel {
    
    private Socket conexao;
    
    private String ip;
    
    private int porta;
    
    private ObjectInputStream streamLeitura;
    
    private ObjectOutputStream streamEscrita;
    
    public ServidorModel() {
    }
    
    public Socket getConexao() {
        return conexao;
    }
    
    public void setConexao(Socket conexao) {
        streamLeitura = null;
        streamEscrita = null;
        this.conexao = conexao;
    }
    
    public String getIp() {
        return ip;
    }
    
    public void setIp(String ip) {
        this.ip = ip;
    }
    
    public int getPorta() {
        return porta;
    }
    
    public void setPorta(int porta) {
        this.porta = porta;
    }
    
    public ObjectInputStream getStreamLeitura() {
        if(streamLeitura==null){
            try {
                this.streamLeitura = new ObjectInputStream(conexao.getInputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("Erro ao tentar criar o canal de Leitura com o servidor!");
            }
        }
        return streamLeitura;
    }
    
    public ObjectOutputStream getStreamEscrita() {
        if(streamEscrita==null){
            try {
                this.streamEscrita = new ObjectOutputStream(conexao.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println("Erro ao tentar criar os canal de Escrita com o servidor!");
            }
        }
        return streamEscrita;
    }
    
    public String toString() {
        return this.ip + ":" + this.porta;
    }
}
